package com.healthtop.web;

import com.healthtop.common.Page;
import com.healthtop.common.Views;
import com.healthtop.dto.RoleMenuDto;
import com.healthtop.dto.UserRoleDto;
import com.healthtop.dto.XsRoleDto;
import com.healthtop.service.DIYService;
import com.healthtop.service.XsRoleMenuService;
import com.healthtop.service.XsRoleService;
import com.healthtop.service.XsUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/xsRole")
public class XsRoleController {

    @Autowired
    private XsRoleService xsRoleService;

    @Autowired
    private DIYService diyService;

    @Autowired
    private XsUserRoleService xsUserRoleService;

    @Autowired
    private XsRoleMenuService xsRoleMenuService;

    @RequestMapping("list")
    public String list(Page page){
        diyService.selectXsRoleList(page);
        return Views.XS_ROLE_LIST;
    }

    @RequestMapping("/forward")
    public String forward(XsRoleDto xsRoleDto) {
        xsRoleService.getForwardValues(xsRoleDto);
        return Views.XS_ROLE_FORM;
    }

    @RequestMapping(value="/operate", method = RequestMethod.POST)
    public String operate(XsRoleDto xsRoleDto) {
        xsRoleService.operate(xsRoleDto);
        return Views.REDIRECT_XS_ROLE_LIST;
    }

    @RequestMapping("/deleteRole")
    public String delete(XsRoleDto xsRoleDto) {
        xsRoleService.deleteRole(xsRoleDto.getRoleId());
        xsRoleMenuService.deleteByRoleId(xsRoleDto.getRoleId());
        xsUserRoleService.deleteByRoleId(xsRoleDto.getRoleId());
        return Views.REDIRECT_XS_ROLE_LIST;
    }

    @RequestMapping("/submitRoleResource")
    @ResponseBody
    public RoleMenuDto submitRoleResource(RoleMenuDto roleMenuDto) {
        xsRoleMenuService.submitRoleMenu(roleMenuDto);
        return roleMenuDto;
    }

    @RequestMapping("/submitUserRole")
    @ResponseBody
    public UserRoleDto submitUserRole(UserRoleDto userRoleDto) {
        xsUserRoleService.submitUserRole(userRoleDto);
       return userRoleDto;
    }
}
